<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />

    <!-- 文章信息区域 -->
    <div class="article-container" v-if="!loading">
      <!-- 文章标题 -->
      <h1 class="art-title">{{ artObj.title }}</h1>

      <!-- 用户信息 -->
      <van-cell center :title="artObj.aut_name" :label="timeAgo(artObj.pubdate)">
        <template #icon>
          <img :src="artObj.aut_photo" alt="avatar" class="avatar" />
        </template>
        <template #default>
          <div>
            <van-button
              @click="followOrunFollowFn(false)"
              v-if="artObj.is_followed"
              type="info"
              size="mini"
              >已关注</van-button
            >
            <van-button
              @click="followOrunFollowFn(true)"
              v-else
              icon="plus"
              type="info"
              size="mini"
              plain
              >关注</van-button
            >
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="artObj.content" ></div>
      <div id="like-box"></div>
      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button
          v-if="artObj.attitude === -1"
          icon="good-job"
          type="danger"
          size="small"
          @click="likeOrDislikeFn(1)"
          >已点赞</van-button
        >
        <van-button
          v-else
          icon="good-job-o"
          type="danger"
          plain
          size="small"
          @click="likeOrDislikeFn(-1)"
          >点赞</van-button
        >
      </div>
    </div>

      <!-- 引入评论组件 -->
      <comment v-if="artObj.art_id" :artObj="artObj"></comment>

  </div>
</template>

<script>
// 引入文章详情接口
import {
  getArticleDetailAPI,
  unfollowAutAPI,
  followAutAPI,
  likeArtAPI,
  dislikeArtAPI
} from '../../api/article'

import { Toast } from 'vant'
// 引入子组件
import comment from './comment.vue'
export default {
  name: 'ArticleDetail',
  components: {
    comment
  },
  data () {
    this.toast = Toast.loading({
      message: '加载中',
      duration: 0
    })
    return {
      // 文章对象
      artObj: {},
      loading: true // 文章详情加载数据
    }
  },

  methods: {

    // 关注和取消关注方法
    async followOrunFollowFn (bln) {
      // 同步到后台
      try {
        bln
          ? await followAutAPI({ artId: this.artObj.aut_id })
          : await unfollowAutAPI({ artId: this.artObj.aut_id })
        Toast.success(bln ? '关注成功!' : '取关成功!')
        // 将获取的值赋值给对象的对应属性
        this.artObj.is_followed = bln
      } catch (err) {
        Toast.fail(bln ? '关注失败!' : '取关失败!')
      }
    },
    // 点赞和取消点赞方法
    likeOrDislikeFn (attitude) {
      // 点击获取值并修改本地状态
      this.artObj.attitude = attitude
      // 修改后台数据
      try {
        attitude === 1
          ? likeArtAPI({ artId: this.artObj.art_id })
          : dislikeArtAPI({ artId: this.artObj.art_id })
        // 成功给出提示
        Toast.success(attitude === 1 ? '取消点赞成功!' : '点赞成功!')
      } catch (err) {
        Toast.fail(attitude === 1 ? '取消点赞失败!' : '点赞失败!')
      }
    }
  },
  async created () {
    //  初始化加载数据
    const res = await getArticleDetailAPI({
      artId: this.$route.query.art_id
    })

    // 数据加载回来后 ,// 提高用户体验
    this.loading = false

    if (!this.loading) {
      // ------------
      this.toast.clear()
    }

    // 赋值给对应的数组
    this.artObj = res.data
    // console.log(this.artObj.content)
  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
  /deep/ img {
    width: 100%;
  }
  /deep/ pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
</style>
